<template>
    <view class="content">
        <image src="/static/pic9.png" mode="widthFix" class="w100"></image>
        <view class="ml30 mr30 line18" style="margin-top: -220rpx;">
            <view class="f45 ">HELLO</view>
            <view class="f45 dis_sc">欢迎使用 <view class="clo-fff bgblue f30 ml20 pl20 pr20 line18">京鑫通达进销存</view>
            </view>
        </view>
        <view class="bgfff rad25 mt60 pt50 pb20">
            <view>账号：</view>
            <view class="dis_sc bgeee rad25 line22 pl20 mt20">
                <text class="iconfont pr10 clo-999">&#xe65d;</text>
                <input type="text" v-model="formData.mobile" placeholder="请输入登录账号">
            </view>
            <view class="mt30">密码：</view>
            <view class="dis_sc bgeee rad25 line22 pl20 mt20 mb40">
                <text class="iconfont pr10 clo-999">&#xe69f;</text>
                <input type="password" v-model="formData.password" placeholder="请输入登录密码" />
            </view>
            <button class="bbg" @click="onSubmit" :disabled="post_lock">立即登录</button>
        </view>

    </view>
</template>
<script setup>
// import { onLoad, onShow } from "@dcloudio/uni-app"
import { ref, reactive, toRefs, onMounted, computed, getCurrentInstance } from 'vue'
import { loginApi } from '@/utils/api'
import { getToken, handleLoginBack } from '@/utils/common'
const { proxy } = getCurrentInstance();
const formData = reactive({
    mobile: "",
    password: "",
})

function limitMobile() {
    let value = formData.mobile;
    const regex = /^[0-9]*$/;
    if (!regex.test(value)) {
        value = value.replace(/[^0-9]/g, '');
    }
    if (value.length > 11) {
        value = value.substring(0, 11);
    }
    formData.mobile = value;
}

function register() {
    uni.navigateTo({
        url: '/pages/register'
    })
}
onMounted(() => {
    let token = getToken();
    if (token) {
        /* 跳到首页 */
        uni.reLaunch({
            url: '/pages/index/index'
        });
        return;
    }
})
const post_lock = ref(false)
function onSubmit() {
    /* 验证一下数据 */
    if (!formData.mobile) {
        proxy.$showToast('请填写账号');
        return false;
    }
    // var reg = /^(1[3-9])\d{9}$/;
    // if (!reg.test(formData.mobile)) {
    //     proxy.$showToast('请填写正确的手机号');
    //     return false;
    // }
    if (!formData.password) {
        proxy.$showToast('请填写密码');
        return false;
    }
    autoLogin(formData.mobile, formData.password);
}

function autoLogin(mobile, password) {
    if (post_lock.value) {
        return false;
    }
    post_lock.value = true;
    let post_data = {};
    post_data.ex_account = mobile
    post_data.ex_password = password
    loginApi(post_data).then(res => {
        uni.setStorageSync('token', res.data.access_token);
        uni.setStorageSync('uid', res.data.user_id);
        setTimeout(() => {
            post_lock.value = false;
        }, 1000)
        handleLoginBack()
        return;
    }).catch(err => {
        setTimeout(() => {
            post_lock.value = false;
        }, 1000)
    })
}
</script>
<style>
	.bgblue {border-radius: 25rpx 0 25rpx 0;}
	.bgfff {padding-left: 20%; padding-right: 20%;}
	button {width: 80%!important; margin: 30rpx 10%; line-height: 2.6; margin-top: 60rpx;}
</style>
